// 文件名称: MenuViewController.java
// 创建日期: 2023-10-05
// 描述: 主菜单视图控制器类，负责处理主菜单界面的各种操作。

package org.wisdom.studentchoose;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;

public class MenuViewController {

    @FXML
    private MenuBar menuBar; // 菜单栏

    @FXML
    private AnchorPane contentPane; // 内容面板

    @FXML
    public void switchAccount() {
        // 确保 contentPane 不为 null
        if (contentPane != null) {
            try {
                // 加载登录界面
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/wisdom/studentchoose/login-view.fxml"));
                Parent loginView = loader.load();

                // 获取当前窗口
                Stage currentStage = (Stage) menuBar.getScene().getWindow();

                // 设置新的场景
                Scene scene = new Scene(loginView);
                currentStage.setScene(scene);
                currentStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("contentPane is null");
        }
    }

    @FXML
    public void exitApp() {
        // 退出应用程序
        System.exit(0);
    }

    @FXML
    public void showClassManagement() {
        // 确保 contentPane 不为 null
        if (contentPane != null) {
            try {
                // 加载班级管理界面
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/wisdom/studentchoose/class-management-view.fxml"));
                Parent classManagementView = loader.load();

                // 获取当前窗口
                Stage currentStage = (Stage) menuBar.getScene().getWindow();

                // 设置新的场景
                Scene scene = new Scene(classManagementView);
                currentStage.setScene(scene);
                currentStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("contentPane is null");
        }
    }

    @FXML
    public void showStudentManagement() {
        // 确认 contentPane 不为 null
        if (contentPane != null) {
            try {
                // 加载学生管理界面
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/wisdom/studentchoose/student-management-view.fxml"));
                Parent studentManagementView = loader.load();
                URL resource = getClass().getResource("/org/wisdom/studentchoose/student-management-view.fxml");
                System.out.println("Resource path: " + resource);

                // 获取当前窗口
                Stage currentStage = (Stage) menuBar.getScene().getWindow();

                // 设置新的场景
                Scene scene = new Scene(studentManagementView);
                currentStage.setScene(scene);
                currentStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("contentPane is null");
        }
    }

    @FXML
    public void showGroupManagement() {
        // 确保 contentPane 不为 null
        if (contentPane != null) {
            try {
                // 加载小组管理界面
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/wisdom/studentchoose/group-management-view.fxml"));
                Parent groupManagementView = loader.load();

                // 获取当前窗口
                Stage currentStage = (Stage) menuBar.getScene().getWindow();

                // 设置新的场景
                Scene scene = new Scene(groupManagementView);
                currentStage.setScene(scene);
                currentStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("contentPane is null");
        }
    }

    @FXML
    public void showClassroomManagement() {
        if (contentPane != null) {
            try {
                // 加载随机学生选择界面
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/wisdom/studentchoose/randomStudentSelection.fxml"));
                Parent classroomManagementView = loader.load();

                // 获取当前窗口
                Stage currentStage = (Stage) menuBar.getScene().getWindow();

                // 设置新的场景
                Scene scene = new Scene(classroomManagementView);
                currentStage.setScene(scene);
                currentStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("contentPane is null");
        }
    }

    public void showGrouproomManagement() {
        if (contentPane != null) {
            try {
                // 加载随机小组选择界面
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/wisdom/studentchoose/randomGroupSelection.fxml"));
                Parent grouproomManagementView = loader.load();

                // 获取当前窗口
                Stage currentStage = (Stage) menuBar.getScene().getWindow();

                // 设置新的场景
                Scene scene = new Scene(grouproomManagementView);
                currentStage.setScene(scene);
                currentStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("contentPane is null");
        }
    }
}
